package com.project.decoration.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.config.utils.ShiroUtils;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.*;
import com.project.decoration.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 项目controller
 * @date 2025/02/16 09:46
 */
@Controller
@ResponseBody
@RequestMapping("project")
public class DecorationProjectController {

    @Autowired
    private DecorationProjectService decorationProjectService;
    @Autowired
    private DecorationAppointmentService decorationAppointmentService;
    @Autowired
    private UserService userService;
    @Autowired
    private DecorationTimeService decorationTimeService;
    @Autowired
    private DecorationTaskService decorationTaskService;

    /** 分页获取项目 */
    @PostMapping("getDecorationProjectPage")
    public Result getDecorationProjectPage(@RequestBody DecorationProject decorationProject) {
        Page<DecorationProject> page = new Page<>(decorationProject.getPageNumber(),decorationProject.getPageSize());
        QueryWrapper<DecorationProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationProject.getPriceId()),DecorationProject::getPriceId,decorationProject.getPriceId())
                .like(StringUtils.isNotBlank(decorationProject.getPriceName()),DecorationProject::getPriceName,decorationProject.getPriceName())
                .eq(StringUtils.isNotBlank(decorationProject.getAppointmentId()),DecorationProject::getAppointmentId,decorationProject.getAppointmentId())
                .like(StringUtils.isNotBlank(decorationProject.getName()),DecorationProject::getName,decorationProject.getName())
                .like(StringUtils.isNotBlank(decorationProject.getTel()),DecorationProject::getTel,decorationProject.getTel())
                .like(StringUtils.isNotBlank(decorationProject.getCity()),DecorationProject::getCity,decorationProject.getCity())
                .like(StringUtils.isNotBlank(decorationProject.getAddress()),DecorationProject::getAddress,decorationProject.getAddress())
                .like(StringUtils.isNotBlank(decorationProject.getCommunity()),DecorationProject::getCommunity,decorationProject.getCommunity())
                .eq(decorationProject.getState() != null,DecorationProject::getState,decorationProject.getState())
                .eq(StringUtils.isNotBlank(decorationProject.getUserId()),DecorationProject::getUserId,decorationProject.getUserId())
                .eq(StringUtils.isNotBlank(decorationProject.getCreateBy()),DecorationProject::getCreateBy,decorationProject.getCreateBy())
                .eq(decorationProject.getCreateTime() != null,DecorationProject::getCreateTime,decorationProject.getCreateTime())
                .eq(StringUtils.isNotBlank(decorationProject.getUpdateBy()),DecorationProject::getUpdateBy,decorationProject.getUpdateBy())
                .eq(decorationProject.getUpdateTime() != null,DecorationProject::getUpdateTime,decorationProject.getUpdateTime());
        Page<DecorationProject> decorationProjectPage = decorationProjectService.page(page, queryWrapper);
        return Result.success(decorationProjectPage);
    }

    @GetMapping("getDecorationProjectList")
    public Result getDecorationProjectList() {
        User userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<DecorationProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationProject::getUserId,userInfo.getId())
                .orderByDesc(DecorationProject::getCreateTime);
        List<DecorationProject> projectList = decorationProjectService.list(queryWrapper);
        return Result.success(projectList);
    }

    @GetMapping("getDecorationProjectFinishList")
    public Result getDecorationProjectFinishList() {
        QueryWrapper<DecorationProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationProject::getState,5)
                .orderByDesc(DecorationProject::getCreateTime);
        List<DecorationProject> projectList = decorationProjectService.list(queryWrapper);
        return Result.success(projectList);
    }

    /** 根据id获取项目 */
    @GetMapping("getDecorationProjectById")
    public Result getDecorationProjectById(@RequestParam("id")String id) {
        DecorationProject decorationProject = decorationProjectService.getById(id);
        return Result.success(decorationProject);
    }

    /** 保存项目 */
    @PostMapping("saveDecorationProject")
    @Transactional(rollbackFor = Exception.class)
    public Result saveDecorationProject(@RequestBody DecorationProject decorationProject) {
        DecorationAppointment appointment = decorationAppointmentService.getById(decorationProject.getAppointmentId());
        decorationProject.setPriceId(appointment.getPriceId());
        decorationProject.setPriceName(appointment.getPriceName());
        decorationProject.setName(appointment.getName());
        decorationProject.setTel(appointment.getTel());
        decorationProject.setCity(appointment.getCity());
        decorationProject.setAddress(appointment.getAddress());
        decorationProject.setCommunity(appointment.getCommunity());
        decorationProject.setArea(appointment.getArea());
        decorationProject.setType(appointment.getType());
        decorationProject.setStyle(appointment.getStyle());
        decorationProject.setIntroduce(appointment.getIntroduce());
        decorationProject.setState(0);
        decorationProject.setUserId(appointment.getUserId());
        User user = userService.getById(appointment.getUserId());
        decorationProject.setCreateBy(user.getUserName());
        appointment.setState(0);
        decorationAppointmentService.updateById(appointment);
        boolean save = decorationProjectService.save(decorationProject);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑项目 */
    @PostMapping("editDecorationProject")
    public Result editDecorationProject(@RequestBody DecorationProject decorationProject) {
        boolean save = decorationProjectService.updateById(decorationProject);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除项目 */
    @GetMapping("removeDecorationProject")
    public Result removeDecorationProject(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                DecorationProject decorationProject = decorationProjectService.getById(id);
                QueryWrapper<DecorationTime> wrapper = new QueryWrapper<>();
                wrapper.lambda().eq(DecorationTime::getProjectId,decorationProject.getId());
                decorationTimeService.remove(wrapper);

                QueryWrapper<DecorationTask> wrapper1 = new QueryWrapper<>();
                wrapper1.lambda().eq(DecorationTask::getProjectId,decorationProject.getId());
                decorationTaskService.remove(wrapper1);
                decorationProjectService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("项目id不能为空！");
        }
    }

}
